package api

import (
	"autoclave/utils/plan"
	"autoclave/utils/tools"
	"github.com/gin-gonic/gin"
	"strconv"
)

type TaskController struct {
}

func (con TaskController) ReceiveDataStart(ctx *gin.Context) {
	interval := ctx.Query("interval")
	val, _ := strconv.Atoi(interval)

	if val == 0 {
		val = 10
	}
	if plan.Task1.State == 0 {
		plan.Task1.State = 1
		plan.Task1.StartTime = tools.GetNow()
		plan.Task1.Interval = val
		go plan.GenerateData()
	} else {

		ctx.JSON(200, gin.H{
			"code":    1,
			"message": "任务正在进行中",
			"data":    plan.Task1,
		})
		return
	}

	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data":    plan.Task1,
	})
}

func (con TaskController) ReceiveDataStop(ctx *gin.Context) {
	if plan.Task1.State == 1 {
		plan.Task1.State = 0
		plan.Task1.EndTime = tools.GetNow()
	}
	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data":    plan.Task1,
	})
}
